Logic or structure of a software and/or hardware program or module may be represented as a control flow graph (CFG). A CFG is a directed graph, in which nodes represent computational statements, elements or expressions, and edges represent transfer of control between nodes. A control flow typically includes all possible execution paths, and each possible execution path of the module has a corresponding path from the entry to the exit node of the graph.
Control flow graphs may be represented graphically and/or textually, or as in-memory representations. There are numerous uses for control flow graphs: code optimization, compilation and interpretation, code visualization, static analysis, dynamic testing, etc. In general, a control flow graph is one of many possible representations of a software and/or hardware module. The CFG may be an intermediate representation, derived from the original representation, or the software or graphical code itself may represent a control flow graph.
A typical control flow graph may have a single entry node and a single exit node, with one or more paths in between, indicating possible flow of execution. Control flow graphs may be viewed as hierarchical—that is, a block in a single entry single exit (SESE) CFG may sometimes be represented as a SESE control flow graph. Conversely, a single entry single exit region of a CFG may be abstracted into one or more blocks in a larger CFG. Such hierarchical change of levels of representation is often used in compilation, optimization, static analysis, and other applications, where it may be useful to analyze a portion of the module at different levels of detail. In some cases, a compiler, optimizer or analyzer may make several paths through a CFG, creating blocks of out sub-graphs or subdividing blocks into SESE sub-graphs.
Control flow graphs may be expressed in a number of ways: graphically, textually or in a combination thereof. CFGs may be a final result of compilation or code generation, or they may be used for code generation. It is usual for an intermediate representation, used in code generation, compilation, execution or analysis, to be a representation of a control flow graph. Moreover, some assembly or hardware representations specifically represent a control flow graph. For example, a section of assembly-level code may be thought as a CFG.